package com.itheima.springbootwebtlias.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

//1.创建切面类,定义类，定义方法
@Component
//2.创建切面，将切面类交给spring管理
@Aspect
public class RecordTimeAspect {
    //定位需要耗时统计的方法
    @Around("execution(* com.itheima.springbootwebtlias.service.impl.*.*(..))")
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {

        LocalDateTime startTime = LocalDateTime.now();
        //调用目标方法
        Object result = pjp.proceed();

        LocalDateTime endTime = LocalDateTime.now();
          //获取方法名
        String methodName = pjp.getSignature().getName();
        System.out.println("AOP方法统计" + methodName + "耗时：" + (endTime.getNano() - startTime.getNano()) + "纳秒");
        return result;
    }
}
